package com.wyp.didishige.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.wyp.didishige.R;
import com.wyp.didishige.model.User;

import cn.bmob.v3.listener.UpdateListener;

/**
 * 设置昵称和性别
 *
 * @author smile
 * @ClassName: SetNickAndSexActivity
 * @Description: TODO
 * @date 2014-6-7 下午4:03:40
 */
public class UpdateInfoActivity extends BaseActivity {

    EditText edit_nick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_updateinfo);
        actionBar = getSupportActionBar();
//        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
        actionBar.setDisplayHomeAsUpEnabled(true);
        initView();
    }

    private void initView() {
        edit_nick = (EditText) findViewById(R.id.edit_nick);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.update_nick,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 修改资料
     * updateInfo
     *
     * @return void
     * @throws
     * @Title: updateInfo
     */
    private void updateInfo(String nick) {
        final User user = userManager.getCurrentUser(User.class);
        User u = new User();
        u.setNick(nick);
        u.setHight(110);
        u.setObjectId(user.getObjectId());
        u.update(this, new UpdateListener() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                final User c = userManager.getCurrentUser(User.class);
                ShowToast("修改成功:" + c.getNick() + ",height = " + c.getHight());
                finish();
            }

            @Override
            public void onFailure(int arg0, String arg1) {
                // TODO Auto-generated method stub
                ShowToast("onFailure:" + arg1);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.update:
                if (edit_nick.getText()==null||edit_nick.getText().toString().equals("")){
                    ShowToast("昵称不能为空");
                }else{
                    updateInfo(edit_nick.getText().toString());
                }

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
